<?php
require_once("Mqtt.php");
$server = "parking.hzgxtc.com";     // 服务代理地址(mqtt服务端地址)
$port = 1883;                     // 通信端口
$username = "A20190619152105";                 // 用户名(如果需要)
$password = "TTTC_2020_ZaoZao";                   // 密码(如果需要M00079
$client_id = "A20190619152105"; // 设置你的连接客户端idM00079
 
$mqtt = new Mqtt($server, $port, $client_id);
 
if(!$mqtt->connect(true, NULL, $username, $password)) { //链接不成功再重复执行监听连接
    exit(1);
}else{
    echo "连接成功\r\n";
}
//hzcity/v2/departurePayInfo/{accessID}/{parkingCode}
//
$topics['/hzcity/v2/departurePayInfo/A20190619152105/#'] = array("qos" => 0, "function" => "procmsg");
$topics['/hzcity/v2/fee/A20190619152105/#'] = array("qos" => 0, "function" => "fee");
$topics['/hzcity/v2/payResult/A20190619152105/#'] = array("qos" => 0, "function" => "payResult");
var_dump($topics);
// 订阅主题为 xxx3809293670ctr qos为0
$mqtt->subscribe($topics, 0);
 
while($mqtt->proc()){
    
}
//死循环监听
$mqtt->close();
 
function procmsg($topic, $msg){ //信息回调函数 打印信息
    if(!empty($msg)){
        $path=dir(getcwd())->path.'/log/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
        echo "Topic: {$topic}\n";
        echo "Msg:{$msg}\n";
        
        
    }  
}

function fee($topic, $msg){ //信息回调函数 打印信息
    if(!empty($msg)){
        echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
        echo "Topic: {$topic}\n";
        echo "Msg:{$msg}\n";
    }  
}

function payResult($topic, $msg){ //信息回调函数 打印信息
    if(!empty($msg)){
        $path=dir(getcwd())->path.'/log/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        echo "Msg Recieved: " . date("Y-m-d H:i:s") . "\n";
        echo "Topic: {$topic}\n";
        echo "Msg:{$msg}\n";
        
    }  
}



